# -*- coding: utf-8 -*-
'''
Created on 12-10-2012

@author: LONG HOANG GIANG
'''

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib import commonlib
from Model.storymodel import StoryModel
from lxml import etree
import glob
import re


class EPub():
    
    _location = ''
    
    def __init__(self, location):
        self._location = location
        if self._location[-1] != '/': self._location += '/'
    
    def getcontentfiles(self, files):
        if files == None or len(files) == 0: return ''
        html = commonlib.file_get_contents(files[0])
        tree = commonlib.build_tree_from_html(html)
        body = tree.xpath("//body")[0]
        for i in range(1, len(files)):
            itree = commonlib.build_tree_from_html(commonlib.file_get_contents(files[i]))
            for item in itree.xpath("//body/*"):
                body.append(item)
        commonlib.Etree.clean_elemtent_by_tag(body, 'iframe')
        output = etree.tounicode(tree, pretty_print=True)
        return output
        
    
    def parse(self):
        tocfile = os.path.join(self._location, 'toc.html')
        if not os.path.exists(tocfile): return
        html = commonlib.file_get_contents(tocfile)
        tree = commonlib.build_tree_from_html(html)
        storymodel = StoryModel(self._location + 'story.sqlite')
        storymodel.open()
        for chapter in tree.xpath("//a"):
            title = commonlib.stringify(chapter)
            htmlfile = chapter.get('href', '')
            if htmlfile == '': continue
            
            print title, htmlfile
            
            if '_split_' in htmlfile:
                patten = re.sub("_\d+\.html", "_*.html", htmlfile)
                htmlfiles = glob.glob(self._location + patten)
                content = self.getcontentfiles(htmlfiles)
            else:
                content = unicode(commonlib.file_get_contents(self._location + htmlfile))
            cid = storymodel.addChapter(unicode(commonlib.toUpper(title)))
            storymodel.addDetail(cid, content)
        storymodel.close()
    
    
    
if __name__ == '__main__':
    
    epub = EPub('C:/Users/LONG HOANG GIANG/Downloads/Steve Jobs - Walter Isaacson_2/')
    epub.parse()
    
    
    